package com.whfc.emp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.emp.dao.AppEmpConfessImgMapper;
import com.whfc.emp.dao.AppEmpConfessMapper;
import com.whfc.emp.dto.AppEmpConfessDTO;
import com.whfc.emp.entity.AppEmpConfess;
import com.whfc.emp.entity.AppEmpConfessImg;
import com.whfc.emp.param.AppEmpConfessAddParam;
import com.whfc.emp.param.AppEmpConfessEditParam;
import com.whfc.emp.service.AppEmpConfessService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-09-02 16:09
 */
@DubboService(interfaceClass = AppEmpConfessService.class, version = "1.0.0")
public class AppEmpConfessServiceImpl implements AppEmpConfessService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppEmpConfessMapper appEmpConfessMapper;

    @Autowired
    private AppEmpConfessImgMapper appEmpConfessImgMapper;

    @Override
    public PageData<AppEmpConfessDTO> list(Integer empId, Integer pageNum, Integer pageSize) throws BizException {
        logger.info("获取安全交底列表, empId:{}, pageNum:{}, pageSize:{}", empId, pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<AppEmpConfessDTO> list = appEmpConfessMapper.selectByEmpId(empId);
        PageHelper.clearPage();
        //获取图片
        for (AppEmpConfessDTO appEmpConfessDTO : list) {
            List<String> imgUrlList = appEmpConfessImgMapper.selectImgUrlByConfessId(appEmpConfessDTO.getConfessId());
            appEmpConfessDTO.setImgUrlList(imgUrlList);
        }
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public void add(AppEmpConfessAddParam param) throws BizException {
        logger.info("新增安全交底, param:{}", param);
        AppEmpConfess confess = new AppEmpConfess();
        BeanUtils.copyProperties(param, confess);
        appEmpConfessMapper.insertSelective(confess);

        //保存安全交底图片
        List<String> imgUrlList = param.getImgUrlList();
        if (imgUrlList == null || imgUrlList.isEmpty()) {
            return;
        }
        saveConfessImg(confess.getId(), imgUrlList);
    }


    @Override
    public void edit(AppEmpConfessEditParam param) throws BizException {
        logger.info("编辑安全交底, param:{}", param);
        AppEmpConfess confess = appEmpConfessMapper.selectByPrimaryKey(param.getConfessId());
        if (confess == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "安全交底信息不存在.");
        }
        BeanUtils.copyProperties(param, confess);
        appEmpConfessMapper.updateByPrimaryKeySelective(confess);

        //清除旧的图片
        appEmpConfessImgMapper.logicDelByConfessId(confess.getId());
        //保存安全交底图片
        List<String> imgUrlList = param.getImgUrlList();
        if (imgUrlList == null || imgUrlList.isEmpty()) {
            return;
        }
        saveConfessImg(confess.getId(), imgUrlList);

    }

    @Override
    public void del(Integer confessId) throws BizException {
        logger.info("删除安全交底, confessId:{}", confessId);
        appEmpConfessMapper.logicDel(confessId);
        //清除图片
        appEmpConfessImgMapper.logicDelByConfessId(confessId);
    }

    @Override
    public void saveConfessSignImg(Integer confessId, String imgUrl) throws BizException {
        logger.info("保存安全交底图片, confessId:{}, imgUrl:{}", confessId, imgUrl);
        appEmpConfessMapper.updateSignImg(confessId, imgUrl);
    }

    /**
     * 保存安全交底图片
     *
     * @param confessId  安全交底ID
     * @param imgUrlList 图片地址
     */
    private void saveConfessImg(Integer confessId, List<String> imgUrlList) {
        for (String imgUrl : imgUrlList) {
            AppEmpConfessImg confessImg = new AppEmpConfessImg();
            confessImg.setConfessId(confessId);
            confessImg.setImgUrl(imgUrl);
            appEmpConfessImgMapper.insertSelective(confessImg);
        }
    }
}
